﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.HRM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GHRMS
{
    [ServiceContract]
    public interface IJobsiteService
    {
        [OperationContract]
        IEnumerable<JobSite> Select(JobSite filter);

        [OperationContract]
        List<SelectListItem> LoadJobSiteSelectList(int? jobSiteId, bool insertBlankAtFirst = true);

        [OperationContract]
        List<SelectListItem> LoadInternalJobSiteSelectList(int? jobSiteId, bool insertBlankAtFirst = true, int Level = 0);

        [OperationContract]
        Employee LoadJobSiteHeadEmployee(int jobSiteId);
       
        [OperationContract]
        string SelectNameJobSite(int jobSiteId);

        [OperationContract]
        List<SelectListItem> JobSiteSelectList(int? jobSiteId);
    }

    [ServiceErrorBehavior(typeof (ElmahErrorHandler))]
    public class JobsiteService : IJobsiteService
    {
        private GERPContext db = new GERPContext();

        public IEnumerable<JobSite> Select(JobSite filter)
        {
            return null;
        }

        public List<SelectListItem> LoadJobSiteSelectList(int? jobSiteId, bool insertBlankAtFirst = true)
        {
            var data = new List<SelectListItem>();

            try
            {
                List<Models.HRM.JobSite> listJobSites = (from m in db.JobSites
                                                         where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                                         select m).ToList();

                if (insertBlankAtFirst) data.Add(new SelectListItem {Text = string.Empty, Value = string.Empty});
                foreach (var jobSite in listJobSites)
                {
                    var item = new SelectListItem {Text = jobSite.Name, Value = jobSite.JobSiteId.ToString()};
                    if (jobSite.JobSiteId == jobSiteId) item.Selected = true;
                    data.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public List<SelectListItem> LoadInternalJobSiteSelectList(int? jobSiteId, bool insertBlankAtFirst = true, int Level = 0)
        {
            var data = new List<SelectListItem>();

            try
            {
                List<Models.HRM.JobSite> listJobSites = (from m in db.JobSites
                                                         where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                                               && m.JobSiteTypeId == 2
                                                               && (Level == 0 ? true : m.JobSiteLevelId == Level)
                                                         select m).ToList();

                if (insertBlankAtFirst) data.Add(new SelectListItem {Text = string.Empty, Value = string.Empty});
                var jobSiteOfRequester = db.JobSites.FirstOrDefault(o => o.JobSiteId == jobSiteId);
                foreach (var jobSite in listJobSites)
                {
                    var item = new SelectListItem {Text = jobSite.Name, Value = jobSite.JobSiteId.ToString()};
                    if (jobSite.JobSiteId == jobSiteId || 
                        (jobSiteOfRequester != null && jobSiteOfRequester.FullPath != null ? jobSiteOfRequester.FullPath : "")
                        .Contains("/" + jobSite.JobSiteId + "/")) item.Selected = true;
                    data.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }

        public Employee LoadJobSiteHeadEmployee(int jobSiteId)
        {
            var jobSite = db.JobSites.FirstOrDefault(j =>
                                                     j.JobSiteId == jobSiteId &&
                                                     j.RecordStatus != RecordStatus.DeletedStatus);
            if (jobSite == null || (jobSite.HeaderPosId == null && string.IsNullOrWhiteSpace(jobSite.FullPath)))
                return null;

            if (jobSite.HeaderPosId != null)
                return db.Employees.FirstOrDefault(o => o.PosId == jobSite.HeaderPosId);


            // sample of path = /2/60/71/96/
            // 1. split
            // 2. exclude empty (first,last)
            // 3. exclude last element (96)
            // 4. answer = 71 60 2
            var jobSitesInPath = (jobSite.FullPath ?? string.Empty).Split('/').Where(o => !string.IsNullOrWhiteSpace(o)).Reverse().Skip(1);

            var jobSiteUppers = from j in db.JobSites
                                where jobSitesInPath.Contains(Convert.ToString(j.JobSiteId))
                                select j;

            foreach (var jobSiteUpper in jobSiteUppers)
            {
                if (jobSiteUpper.HeaderPosId != null)
                {
                    return db.Employees.FirstOrDefault(o => o.PosId == jobSiteUpper.HeaderPosId);
                }
            }
            return null;
        }

        public string SelectNameJobSite(int jobSiteId)
        {

             try
            {
                string Tmp = null;
                if (jobSiteId != 0)
                {
                    string NameJobSites = (from m in db.JobSites
                                           where m.JobSiteId == jobSiteId
                                           select m.Name).First().ToString();
                    Tmp = NameJobSites;
                }
                else
                {
                    Tmp = "";
                }
                return Tmp;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<SelectListItem> JobSiteSelectList(int? jobSiteId)
        {
            var data = new List<SelectListItem>();

            try
            {
                List<Models.HRM.JobSite> listJobSites = (from m in db.JobSites
                                                         where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                                         select m).ToList();
                foreach (var jobSite in listJobSites)
                {
                    var item = new SelectListItem { Text = jobSite.Name, Value = jobSite.JobSiteId.ToString() };
                    if (jobSite.JobSiteId == jobSiteId) item.Selected = true;
                    data.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            return data;
        }
    }
}